const {
  http
} = require("../../../../utils/apiutils")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNo: 0,
    total: 0,
    dataList: [],
    tabActive:1,
    hasMore:true,
    loading:false,
    jrxz:0,
    total:0,
    isCanDraw:false,
    code_img:'',
    bg_img:'https://linghuoimage.xbbzp.com/FgIjAy49uinOBUTKsf2pRCr024Ca',
    // bg_img:'',
    obj:{
      width:"750",
      height:"1564",
      left:"275",
      bottom:"120",
      code_width:200,
      code_height:200,
      btn_background:'#5B0EFE'
    }, // 分享海报所需参数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getinvite();
  },
  onclickCall1: function (e) {
    let mo = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: mo,
    })
    
  },
  ages: function (str) {
    var r = str.match(/^(\d{1,4})(\d{1,2})(\d{1,2})$/);
    console.log("-----", r);
    if (r == null) return "未知";
    var d = new Date(r[1], r[2] - 1, r[3]);
    console.log("-----------", d);
    if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[2] && d.getDate() == r[3]) {
      var D = new Date();
      var Y = D.getFullYear();
      var M = D.getMonth() - 1;
      var nf = Y - r[1];
      var yf = 0
      if (M - r[2] > 0) {
        yf = M - r[2];
      } else {
        nf = nf - 1;
        yf = M + 12 - r[2];
      }
      return (nf + "岁" + yf + "个月");
    }
    return "未知";
  },

  getinvite() {
    var data = {
      "pageNo": this.data.pageNo+1, //手机号码
      "pageSize": "10", //密码
      type: this.data.tabActive
    }
    if(this.data.loading){
      return false;
    }
    if(this.data.loading) return false;
    if(!this.data.hasMore){
      wx.showToast({
        title: '没有更多数据',
        icon:"none"
      })
      return false;
    }
    this.data.loading = true;
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    http.sharePost(this.data.tabActive,
       (res)=>{
        wx.hideLoading()
        console.log("------s收藏岗位列表---", res);
        if (res.code == 0) {
          var pageNo = res.data.current;
          var total = res.total;
          var arr = res.data;

          if (arr.length>0){
             arr.forEach(function (item) {
               item.nl = item.age
             });
          }
          that.setData({
            dataList: arr,
            pageNo,
            total,
            jrxz:res.jrxz
          })
          if (pageNo == 1) {
            that.setData({
              dataList: arr,
              pageNo,
              total
            })
          } else if (pageNo <= total) {
            let hasMore=true;
            if (pageNo == total) hasMore=false;
            that.setData({
              dataList: [...that.data.dataList, ...arr],
              pageNo,
              total,
              hasMore
            })
          }
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
         that.data.loading = false;
      },
      function (err) {
        wx.hideLoading()
      })

  },
  tabChange(event){
     let title = event.detail.title;
     console.log(event)
     this.setData({
       pageNo: 0,
       total: 0,
       dataList: [],
       tabActive: event.detail.name
     })
     wx.nextTick(() => {
       this.getinvite()
     })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var shareObj = {
      title: "分享岗位", // 默认是小程序的名称(可以写slogan等)
      path: '/pages/index/index?id='+getApp().globalData.userInfo.id+'&event=1', // 
      imageUrl: '',
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
        }
      }
    }
    console.log("------------", shareObj);
    // 返回shareObj
    return shareObj;
  },
  getShareCode(){
    wx.showLoading()
		let params = {
			id: getApp().globalData.userInfo.id,
			event:1
		}
		http.createQrcode(params,res=>{
			wx.hideLoading({})
			let src = 'data:image/jpeg;base64,'+res;
			this.setData({
				code_img: src
			})
			this.createShareImage();
		},err=>{
			wx.hideLoading({})
			wx.showToast({
			  title: '请重新点击',
			  icon:"none"
			})
		})
  },
  createShareImage() {
		this.setData({
			isCanDraw: !this.data.isCanDraw 
		})
	},
	toHx(e) {
		if (this.data.tabActive == 1) { 
			return false;
		}
		wx.navigateTo({
		  url: '/pages/clock/pages/personalInfo/personalInfo?id='+e.currentTarget.dataset.id,
		})
	 }
})